package com.joint.cloud.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lilengyi
 * @date 2020/4/10 15:50
 */
public class UserUtil {
    public static String getCurrentUser(){
        if (null != RequestContextHolder.getRequestAttributes()) {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            if (null != request.getHeader("username")) {
                // 也可获取到roles userId
//                request.getHeader("roles");
//                request.getHeader("userId");
                return request.getHeader("username");
            }
        }
        return "system";
    }
    
    
    public static String getCurrentIp(){
    	if(null != RequestContextHolder.getRequestAttributes()) {
    		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    		String ip =request.getHeader("X-Real-IP");
    		if(!StringUtils.hasLength(ip)) {
    			return null;
    		}
            return ip;
    	}
    	return "system";
    }
}
